package cn.hyatt.auth.service.impl;

import cn.hyatt.auth.entity.SystemUserRole;
import cn.hyatt.auth.mapper.SystemUserRoleMapper;
import cn.hyatt.auth.service.SystemUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户-服务实现
 *
 * @author hyatt
 */
@Slf4j
@Service
@Primary
@AllArgsConstructor
public class SystemUserRoleServiceImpl extends ServiceImpl<SystemUserRoleMapper, SystemUserRole> implements SystemUserRoleService {

    @Override
    @Transactional
    public void saveUserRoles(Long userId, List<Long> roleIds) {
        List<SystemUserRole> list = roleIds.stream().map(roleId -> {
            SystemUserRole roleMenu = new SystemUserRole();
            roleMenu.setUserId(userId);
            roleMenu.setRoleId(roleId);
            return roleMenu;
        }).toList();
        saveBatch(list);
    }

    @Override
    @Transactional
    public void deleteUserRoles(Long userId) {
        remove(new LambdaQueryWrapper<SystemUserRole>()
                .eq(SystemUserRole::getUserId, userId)
        );
    }
}
